package simple14.template;


import simple14.discount.Discount;
import simple14.fruit.Fruit;

import java.util.ArrayList;
import java.util.List;

public abstract class ShoppingCart {
    List<Fruit> products = new ArrayList<>();
    private Discount discount;

    public void setDiscount(Discount discount) {
        this.discount = discount;
    }

    public ShoppingCart(List<Fruit> products) {
        this.products = products;
    }
    
    public void  submitOrder(){
//        计算金额
        int money = balance();
        System.out.println("需要支付的总金额为:"+money);

        money = (int) discount.calculate(money);
        System.out.println("优惠减免后:"+money+"元");
//        保存订单
        pay(money);
//        送货上门
        sendHome();
    }

    protected abstract void pay(int money);

    protected  void sendHome(){
        System.out.println("三公里以内免费送货上门");
    }

    private int balance() {
        int money = 0;
        System.out.println("商品清单");
        for (Fruit product : products) {
            product.fruit();
            System.out.println(",");
            money+= product.price();

        }
        System.out.println("金额"+money);


        return money;
    }
}
